<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .father {
            position: relative;
            width: 200px;
            height: 200px;
            background-color: pink;
            margin: 150px;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: purple;
            margin-left: 45px;
        }
        
        .w {
            height: 200px;
            background-color: skyblue;
            margin: 0 auto 200px;
            padding: 10px;
            border: 15px solid red;
        }
    </style>
</head>

<body>
    <script>
        window.addEventListener('load', () => {
            let father = document.querySelector(".father");
            let son = document.querySelector(".son")
                //返回元素的偏移量
            console.log(father.offsetTop);
            console.log(father.offsetLeft)
                //以带有定位的父亲为准
            console.log(son.offsetLeft)
                // 可以得到元素的大小 宽度和高度 是包含padding + border + width
            console.log(document.querySelector('.w').offsetWidth)
            console.log(document.querySelector(".w").offsetHeight)
                //返回带有定位的父亲
            console.log(document.querySelector(".son").offsetParent)
                //返回最近的上一级
            console.log(document.querySelector(".son").parentNode)
        })
    </script>
    <div class="father">
        <div class="son">

        </div>

    </div>
    <div class="w">

    </div>


</body>

</html>